// 1160. 拼写单词

#include <unordered_map>
#include <string>
#include <vector>
using namespace std;

class Solution
{
public:
    int countCharacters(vector<string>& words, string chars) 
    {
        unordered_map<char, int> chars_cnt;
        for(auto& ch : chars) chars_cnt[ch]++;

        int ret = 0;
        for(auto& word : words)
        {
            unordered_map<char, int> word_cnt;
            bool flag = true;
            for(auto& ch : word)
            {
                word_cnt[ch]++;
                if(chars_cnt[ch] < word_cnt[ch]) 
                {
                    flag = false;
                    break;
                }
            }
            if(flag) ret += word.size();
        }
        return ret;
    }
};